package com.cyt.website.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.website.dao.WebsiteFriendlyLinkDao;
import com.cyt.website.model.po.WebsiteFriendlyLink;
import com.cyt.website.model.vo.WebsiteFriendlyLinkVo;
import com.cyt.website.service.WebsiteFriendlyLinkService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 官网友情链接-官网友情链接Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-12 14:16:01
 */
@Service("websiteFriendlyLinkService")
public class WebsiteFriendlyLinkServiceImpl extends BaseServiceImpl<WebsiteFriendlyLink> implements WebsiteFriendlyLinkService {

    @Resource
    private WebsiteFriendlyLinkDao websiteFriendlyLinkDao;

    @Autowired
    public void setBaseDao(WebsiteFriendlyLinkDao websiteFriendlyLinkDao) {
        this.baseDao = websiteFriendlyLinkDao;
    }

    /**
     * 查询对象集合
     *
     * @param websiteFriendlyLinkVo 对象
     * @return List 对象集合
     * @author congcong tao
     * @date 2019-8-12 14:16:01
     */
    @Override
    public List<WebsiteFriendlyLink> listExample(WebsiteFriendlyLinkVo websiteFriendlyLinkVo) {
        Example example = new Example(WebsiteFriendlyLink.class);
        example.setOrderByClause(" order_num asc");
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(websiteFriendlyLinkVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", websiteFriendlyLinkVo.getRemoveTag());
        }
        if (StrUtil.isNotEmpty(websiteFriendlyLinkVo.getName())) {
            criteria.andLike("name", "%" + websiteFriendlyLinkVo.getName() + "%");
        }
        return websiteFriendlyLinkDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param websiteFriendlyLinkVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-12 14:16:01
     */
    @Override
    public ReturnResult page(WebsiteFriendlyLinkVo websiteFriendlyLinkVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteFriendlyLink> list = listExample(websiteFriendlyLinkVo);
        PageInfo<WebsiteFriendlyLink> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param websiteFriendlyLinkVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-12 14:16:01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WebsiteFriendlyLinkVo websiteFriendlyLinkVo, SysUser sysUser) {
        if (StrUtil.isEmpty(websiteFriendlyLinkVo.getId())) {
            WebsiteFriendlyLink websiteFriendlyLink = new WebsiteFriendlyLink(sysUser);
            websiteFriendlyLink.setCodeName(websiteFriendlyLinkVo.getCodeName());
            websiteFriendlyLink.setName(websiteFriendlyLinkVo.getName());
            websiteFriendlyLink.setUrl(websiteFriendlyLinkVo.getUrl());
            websiteFriendlyLink.setStatus(websiteFriendlyLinkVo.getStatus());
            websiteFriendlyLink.setOrderNum(websiteFriendlyLinkVo.getOrderNum());
            websiteFriendlyLink.setSeoKeywords(websiteFriendlyLinkVo.getSeoKeywords());
            websiteFriendlyLink.setSeoDescription(websiteFriendlyLinkVo.getSeoDescription());
            int num = websiteFriendlyLinkDao.insert(websiteFriendlyLink);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WebsiteFriendlyLink websiteFriendlyLink = websiteFriendlyLinkDao.selectByPrimaryKey(websiteFriendlyLinkVo.getId());
            websiteFriendlyLink.setCodeName(websiteFriendlyLinkVo.getCodeName());
            websiteFriendlyLink.setName(websiteFriendlyLinkVo.getName());
            websiteFriendlyLink.setUrl(websiteFriendlyLinkVo.getUrl());
            websiteFriendlyLink.setStatus(websiteFriendlyLinkVo.getStatus());
            websiteFriendlyLink.setOrderNum(websiteFriendlyLinkVo.getOrderNum());
            websiteFriendlyLink.setSeoKeywords(websiteFriendlyLinkVo.getSeoKeywords());
            websiteFriendlyLink.setSeoDescription(websiteFriendlyLinkVo.getSeoDescription());
            websiteFriendlyLink.setUpdateTime(new Date());
            websiteFriendlyLink.setUpdateUser(sysUser.getId());
            int num = websiteFriendlyLinkDao.updateByPrimaryKey(websiteFriendlyLink);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param websiteFriendlyLinkVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-12 14:16:01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WebsiteFriendlyLinkVo websiteFriendlyLinkVo, SysUser sysUser) {
        //要修改的字段
        WebsiteFriendlyLink websiteFriendlyLink = new WebsiteFriendlyLink();
        websiteFriendlyLink.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        websiteFriendlyLink.setUpdateUser(sysUser.getId());
        websiteFriendlyLink.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WebsiteFriendlyLink.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteFriendlyLinkVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteFriendlyLinkVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        websiteFriendlyLinkDao.updateByExampleSelective(websiteFriendlyLink, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param websiteFriendlyLinkVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-12 14:16:01
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WebsiteFriendlyLinkVo websiteFriendlyLinkVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WebsiteFriendlyLink.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteFriendlyLinkVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteFriendlyLinkVo.getId().split(",")));
        }
        websiteFriendlyLinkDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}